package com.sumanth.lvdistance;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

import java.lang.Math;

public class LVActivity extends Activity {

	private Button B1C,B2E;
	private TextView RES;
	private EditText ST1,ST2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lv);
        
        addClickListener();
    }

    public void addClickListener(){
    	B1C=(Button) findViewById(R.id.button1);
    	B2E=(Button) findViewById(R.id.button2);
    	RES=(TextView) findViewById(R.id.textView3);
    	B1C.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ST1=(EditText) findViewById(R.id.editText1);
				ST2=(EditText) findViewById(R.id.editText2);
				String sa=ST1.getText().toString();
				String sb=ST2.getText().toString();
				int R1=LV(sa,sb);
				double R2=(double) 1/(1+ R1);
				String str1=Integer.toString(R1);
    			String str2=Double.toString(R2);
    			RES.setText("Edit Distance:: "+ str1 +"\nSimilarity:: "+str2 );
			}
		} );
    	B2E.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
    	
    	
    }
    
    public int LV(String sa1, String sa2){
    	 int la1=sa1.length();
    	 int la2=sa2.length();
    	 int cost=0;
    	 int i;
    	if (la1==0){
    		i=la2;
    	}
    	else {
    		if(la2==0){
    			i=la1;
    		}
    		else {
    			if(!(sa1.substring(0,1).equals(sa2.substring(0,1)))){
    				
    				cost=1;
    			}
    			int i1=LV(sa1.substring(1,la1), sa2)+1;
    			int i2=LV(sa1, sa2.substring(1,la2))+1;
    			int i3=LV(sa1.substring(1,la1), sa2.substring(1,la2))+cost;
    			i=Math.min(Math.min(i1, i2),i3);
    			
    		}
    	}
    	
    	return i;
    }
    
}

